package com.louis.testvalidationtwo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;

import java.util.List;

/**
 * @author XRY
 * @date 2023年06月29日14:39
 */

@Service
public class MyValidationTwo {
    //使用spring中的validation
    @Autowired
    private Validator validator;

    public List testValidatorTwo(User user){
        BindException bindException = new BindException(user, user.getName());
        validator.validate(user, bindException);
        List<ObjectError> allErrors = bindException.getAllErrors();
        return allErrors;
    }
}
